package cn.nawang.ebeim.ui.controller;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.nawang.ebeim.client.bean.ClientConfiguration;
import cn.nawang.ebeim.ui.util.ApiUtil;
import cn.nawang.ebeim.ui.util.FileOprt;

/**
 * @ClassName: PushMessageController
 * @Description: TODO
 * @author fanyb
 * @date Nov 12, 2015 5:40:09 PM
 * @version v1.0
 */
@RestController
public class PushMessageController {
  private static final Logger LOG = LoggerFactory.getLogger(PushMessageController.class);
  
  /**
   * @author zhangjf
   * @Description: 获取推送消息
   * @param @return
   * @return String
   * @date 2015年10月29日上午8:43:50
   *
   * @修改人 zhangjf
   * @修改日期
   */
  @RequestMapping(value = "/pushmessage/list", method = RequestMethod.GET)
  public String getPushMessage() {
    String id = FileOprt.readFile(
        ClientConfiguration.getWorkingPath() + File.separator + FileOprt.PUSH_MESSAGE_FILENAME);
    if (id.equals("")) {
      id = "0";
    }
    String result = ApiUtil.getPushMessage(id);
    JSONObject json = JSONObject.parseObject(result);
    String code = json.getString("code");
    JSONArray jsonArr = json.getJSONArray("list");
    if (code.equals("200") && jsonArr.size() > 0) {
      JSONObject jsonBean = jsonArr.getJSONObject(0);
      id = String.valueOf(jsonBean.get("id"));
      FileOprt.writeFileCover(
          ClientConfiguration.getWorkingPath() + File.separator + FileOprt.PUSH_MESSAGE_FILENAME,
          id);
    }
    LOG.debug("getBackupLog: {}", result);
    return result;
  }
}
